<?php
abstract class Pageley_Commerce_DomainTests extends Pageley_Test_Case {
	private $factory;

    public function setUp() {
        Pageley_Config::reset();
        $factory = Pageley_Config::getInstance();
        $factory->addConfigLocation("basic/attributes");
        $factory->addConfigLocation("basic/controllers");
        $factory->addConfigLocation("basic/validators");
		$factory->addConfigLocation("commerce/environment-unittest");
        $factory->addConfigLocation("commerce/application");
		$factory->addConfigLocation("commerce/attributes");
		$factory->addConfigLocation("commerce/types");
		$factory->initialize();
        $this->factory = $factory;
        $gateway = $factory->valueOf("gateway");
        
        $category1 = new Pageley_Commerce_Domain_Category();
        $category1->set("name", "Animals");
        $gateway->insert($category1);
        
        $category2 = new Pageley_Commerce_Domain_Category();
        $category2->set("name", "Animal sprays");
        $category2->set("parentCategory", $category1);
        $gateway->insert($category2);
        
        $category3 = new Pageley_Commerce_Domain_Category();
        $category3->set("name", "Animal gadgets");
        $category2->set("parentCategory", $category1);
        $gateway->insert($category3);
        
        $supplier = new Pageley_Commerce_Domain_Supplier();
        $supplier->set("code", "SPF");
        $supplier->set("companyName", "SprayFab");
        $supplier->set("address", new Pageley_Basic_Domain_Address("1001, Factory Street", null, "Rhinopolis", "39339"));
        $supplier->set("vatCode", "ZZ0000000001");
        $supplier->set("emailAddress", "info@sprayfab.com");
        $supplier->set("websiteUrl", "http://www.sprayfab.com/");
        
        $article1 = new Pageley_Commerce_Domain_Article();
        $article1->set("name", "Llama Spray");
        $article1->set("price", new Pageley_Basic_Domain_Money(Pageley_Basic_Domain_Currency::$EUR, 2.20));
        $article1->set("supplier", $supplier);
        $article1->set("active", true);
        $article1->set("category", $category1);
        $article1->set("date", Pageley_Util_Timestamp::parse("2008-01-05 12:00:00"));
        $gateway->insert($article1);
        
        $article2 = new Pageley_Commerce_Domain_Article();
        $article2->set("name", "Frog Spray");
        $article2->set("price", new Pageley_Basic_Domain_Money(Pageley_Basic_Domain_Currency::$EUR, 1.30));
        $article2->set("supplier", $supplier);
        $article2->set("active", true);
        $article2->set("category", $category1);
        $article2->set("date", Pageley_Util_Timestamp::parse("2008-01-05 11:59:59"));
        $gateway->insert($article2);
        
        $article3 = new Pageley_Commerce_Domain_Article();
        $article3->set("name", "Cobra Spray");
        $article3->set("price", new Pageley_Basic_Domain_Money(Pageley_Basic_Domain_Currency::$EUR, 5.50));
        $article3->set("supplier", $supplier);
        $article3->set("active", true);
        $article3->set("category", $category1);
        $article3->set("date", Pageley_Util_Timestamp::parse("2008-01-05 12:00:01"));
        $gateway->insert($article3);
        
        $customer = new Pageley_Commerce_Domain_Customer();
        $customer->set("contactName", "Bill Gates");
        $customer->set("address", new Pageley_Basic_Domain_Address("1, Gateway", null, "Micropolis", "9999"));
        $customer->set("emailAddress", "bill.gates@hotmail.com");
        $customer->set("status", Pageley_Commerce_Domain_CustomerStatus::$ACTIVE);
        $customer->set("username", "bill.gates");
        $customer->set("password", Pageley_Serializer_Format::$HTTP->getSerializer()->encrypt("irock"));
        $gateway->insert($customer);
    }

    public function tearDown() {
    	$factory = Pageley_Controller_Session_Factory::getInstance();
        $session = $factory->getSession();
        $session->remove("commerce.order");
    }
    
    protected function dispatchRequest($parameters) {
        $dispatcher = $this->factory->valueOf("requestDispatcher");
        $dispatcher->setViewResolver(new Pageley_Controller_ViewResolver_NoView());
        return $dispatcher->dispatchRequest(new Pageley_Util_ArrayMap($parameters));
    }
}